﻿// -----------------------------------------------------------------------------
// 园丁,是个很简单的管理系统
//  gitee:https://gitee.com/hgflydream/Gardener 
//  issues:https://gitee.com/hgflydream/Gardener/issues 
// -----------------------------------------------------------------------------

namespace Gardener.Weighbridge.Impl.Core
{
    /// <summary>
    /// 上载数据解析
    /// </summary>
    public class WeighbridgeUploadDataParse
    {
        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static WeighbridgeUploadData? Parse(byte[] data)
        {
            int channel = data[0];
            int functionCode = data[1];
            if (functionCode == 3)
            {
                //读值
                int length = data[2];
                WeighbridgeUploadData weighbridgeData = new WeighbridgeUploadData(UploadDataType.ReadValue, channel);
                int index = 0;
                int[] datas = new int[length / 2];
                while (index < length / 2)
                {
                    int begin = (index * 2) + 3;
                    int end = begin + 1;

                    if (index == 0)
                    {
                        //重量
                        Int16 int16 = BitConverter.ToInt16([data[end], data[begin]], 0);
                        weighbridgeData.Weight = int16;
                    }
                    else if (index == 1)
                    {
                        //小数点
                        weighbridgeData.PrecisionType = PrecisionType.GetPrecisionType(data[end]);

                    }
                    else if (index == 2)
                    {
                        //单位
                        weighbridgeData.UnitType = UnitType.GetUnitType(data[end]);

                    }
                    else if (index == 17)
                    {
                        //去皮
                        weighbridgeData.NetWeight = data[end] == 01;
                    }

                    index++;
                }

                return weighbridgeData;
            }
            else if (functionCode == 6)
            {
                UploadDataType uploadDataType = new UploadDataType(data[2], data[3]);
                WeighbridgeUploadData weighbridgeData = new WeighbridgeUploadData(uploadDataType, channel);
                if (UploadDataType.Unit.Equals(uploadDataType))
                {
                    weighbridgeData.UnitType = UnitType.GetUnitType(data[5]);
                }
                else if (UploadDataType.Precision.Equals(uploadDataType))
                {
                    weighbridgeData.PrecisionType = PrecisionType.GetPrecisionType(data[5]);
                }
                else if (UploadDataType.Netweight.Equals(uploadDataType))
                {
                    weighbridgeData.NetWeight = data[5] == 01;
                }

                return weighbridgeData;
            }
            return null;

        }
    }
}
